package com.core.springSecurity.service;

import com.common.ErrorCodeEnum;
import com.common.emums.UserType;
import com.common.exception.BusinessException;
import com.common.utils.StringUtils;
import com.core.springSecurity.common.SecurityUtils;
import com.core.springSecurity.model.LoginUserDetails;
import com.model.pojo.sys.SysMenu;
import com.model.pojo.sys.SysRole;
import com.model.pojo.sys.SysUser;
import com.service.sys.SysDeptService;
import com.service.sys.SysMenuService;
import com.service.sys.SysRoleMenuService;
import com.service.sys.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

@Slf4j
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysPermissionService permissionService;

    @Resource
    private SysRoleMenuService sysRoleMenuService;

    @Resource
    SysMenuService sysMenuService;

    @Resource
    SysDeptService sysDeptService;

    @Override
    public UserDetails loadUserByUsername(String username) {
        // 从数据库中取出用户信息
        SysUser user;
        try {
            user = sysUserService.getByNameOrMobileOrEmail(username);
            if(StringUtils.isNull(user)){
                throw new BusinessException(ErrorCodeEnum.USER_DATA_NOT_FOUND);
            }
        }catch (BusinessException e){
            log.error("登录用户：{} 不存在.", username);
            throw new BusinessException(ErrorCodeEnum.USER_FAILED);
        }
//        if(UserType.ANONYMOUS.equals(user.getUserType())){
//            log.info("禁止游客使用账号密码登录：{}", username);
//            throw new BusinessException(ErrorCodeEnum.BAN_ANONYMOUS_LOGIN);
//        }
        if(user.getStatus() == 0){
            log.info("该帐号已被禁止登录：{}", username);
            throw new BusinessException(ErrorCodeEnum.USER_FORBID);
        }

        // 返回UserDetails实现类
        return createLoginUser(user);
    }

    public UserDetails createLoginUser(SysUser user) {
        //记录用户信息并添加对应角色权限
        Set<SysRole> sysRoleSet= permissionService.getRoles(user);
        //根据角色读取菜单列表
        Set<SysMenu> sysMenuSet;
        if(SecurityUtils.isAdmin(user.getSysUserId())){
            sysMenuSet = new HashSet<>(sysMenuService.getList());
        }else{
            sysMenuSet= sysRoleMenuService.getMenuSet(sysRoleSet);
        }

        return new LoginUserDetails(
                //用户基本信息
                user,
                //接口权限列表
                permissionService.getRolePermission(user,sysRoleSet),
                //按钮权限列表
                permissionService.getRoleButton(user,sysRoleSet),
                //角色列表
                sysRoleSet,
                //菜单列表
                sysMenuSet,
                sysDeptService.get(user.getSysDeptId(),null).getDeptName());
    }
}
